<?php
/**
 * @author wyl
 * @date 2015.7.15
 * @todo  valid su class
 */
Yii::import('application.components.register.class.BaseRegisterForm');
class RegisterForm extends BaseRegisterForm{
	public $email;
	public $user_name;
	public $password;
	public $is_agree;
	public $verifyCode;
	public $customerId;
	
	public function rules(){
		return array(
			
			 array('password,email,user_name,verifyCode','required'),
			 array('user_name','length','min'=>5),
			 array('user_name','match','pattern'=>'/[0-9A-Za-z]+/','message'=>
					'Usernames must be at least 5 characters, and alphanumeric only (A-Z / 0-9). No spaces or special characters are allowed.'
		), 
			array('password','length','min'=>6,'max'=>15),
			array('email','email'),
			array('user_name','unique','className'=>'Customer'),
			array('email','unique','className'=>'Customer'),
			array('is_agree','in','range'=>array(1),'message'=>'please agree the privacy policy!'),
			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
			//array('verifyCode','captcha', 'on'=>'login','allowEmpty'=>!Yii::app()->user->isGuest),
		);
	}
	
	public function register(){

		$valid=$this->valid();
		if($valid){

			$model=new Customer();
			$model->attributes=$this->attributes;
			$model->password=$this->password;
			$model->created=time();
			$model->status=1;
			$result=$model->save();
			$this->customerId = $model->id;

			return $result;
		} 
		return $valid;
	}
	
}